package org.hhf.ai.controller;

import org.apache.commons.lang3.StringUtils;
import org.hhf.ai.pojo.AiChatForm;
import org.hhf.ai.service.AiChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.UUID;

@RequestMapping("/ai")
@RestController
public class AiChatController {

    @Autowired
    private AiChatService aiChatService;

    @PostMapping(value="/sseChat")
    public SseEmitter sseChat(@RequestBody AiChatForm form) {
        Assert.notNull(form, "参数不能为空");
        Assert.notNull(form.getConversationId(), "conversationId参数不能为空");
        Assert.notNull(form.getTenantId(), "tenantId参数不能为空");
        Assert.isTrue(StringUtils.isNotBlank(form.getPrompt()),"用户输入不能为空");
        String prompt = form.getPrompt()+" 当前日期:{today}，当前用户Id:{userId}，当前租户Id:{tenantId}";
        prompt = prompt.replace("{today}", "2025-02-27")
                .replace("{userId}", "1")
                .replace("{tenantId}", form.getTenantId().toString());
        form.setPrompt(prompt);
        form.setSessionId(UUID.randomUUID().toString());
        return aiChatService.sseChat(form);
    }
}
